import dayjs from 'dayjs'

type CompareType = 'year-on-year' | 'month-on-month'

type DateRange = [Date, Date]

/**
 * 获取同比和环比时间
 * @param compareType
 * @param range
 * @param type
 */
export function getDateRange(
  compareType: CompareType,
  range: DateRange,
  type: 'day' | 'week' | 'month' | 'year',
): [string, string] {
  const [startDate, endDate] = range
  const addDays = (date: Date, days: number): Date => {
    const result = dayjs(date)
    return result.add(days, 'day').toDate()
  }

  const addWeeks = (date: Date, weeks: number): Date => {
    return addDays(date, weeks * 7)
  }

  const addMonths = (date: Date, months: number): Date => {
    const result = dayjs(date)
    return result.add(months, 'month').toDate()
  }

  const addYears = (date: Date, years: number): Date => {
    const result = dayjs(date)
    return result.add(years, 'year').toDate()
  }

  let newStartDate: Date
  let newEndDate: Date

  switch (compareType) {
    case 'year-on-year':
      newStartDate = addYears(startDate, -1)
      newEndDate = addYears(endDate, -1)
      break

    case 'month-on-month':
      if (type == 'week') {
        newStartDate = addWeeks(startDate, -1)
        newEndDate = addWeeks(endDate, -1)
      } else if (type == 'month') {
        newStartDate = addMonths(startDate, -1)
        newEndDate = addMonths(endDate, -1)
      } else if (type == 'year') {
        newStartDate = addYears(startDate, -1)
        newEndDate = addYears(endDate, -1)
      } else {
        newStartDate = addDays(startDate, -1)
        newEndDate = addDays(endDate, -1)
      }
      break

    default:
      throw new Error('Invalid compare type')
  }
  return [
    dayjs(newStartDate).format('YYYY-MM-DD HH:mm:ss'),
    dayjs(newEndDate).format('YYYY-MM-DD HH:mm:ss'),
  ]
}

export function getTimeRange(now: dayjs.Dayjs, type: 'day' | 'week' | 'month') {
  let startDate: dayjs.Dayjs
  let endDate = now.add(0, 'day')

  switch (type) {
    case 'day':
      startDate = now.add(0, 'day')
      break
    case 'week':
      startDate = now.startOf('week')
      endDate = now.endOf('week')
      break
    case 'month':
      startDate = now.startOf('month')
      endDate = now.endOf('month')
      break
    default:
      startDate = now.add(-1, 'day')
      break
  }
  return [startDate.format('YYYY-MM-DD 00:00:00'), endDate.format('YYYY-MM-DD 23:59:59')]
}

export function getRangeType(range: [Date, Date]) {
  const start = range[0]
  const end = range[1]
  const sd = start.getDate()
  const sm = start.getMonth()
  const sy = start.getFullYear()
  const ed = end.getDate()
  const em = end.getMonth()
  const ey = end.getFullYear()

  if (sd == ed && sm == em && sy == ey) {
    return 'day'
  } else if (sy == ey && sm == em) {
    return 'month'
  } else if (sy == ey) {
    return 'year'
  }

  return 'year'
}

export function getDateRangeStr(range: [dayjs.Dayjs, dayjs.Dayjs]) {
  const d1 = range[0]
  const d2 = range[1]

  const d1d = d1.format('YYYY-MM-DD')
  const d1m = d1.format('YYYY年MM月')
  const d1y = d1.format('YYYY年')

  const d2d = d2.format('YYYY-MM-DD')
  const d2m = d2.format('YYYY年MM月')
  const d2y = d2.format('YYYY年')

  if (d1d == d2d) {
    return d1.format('YYYY-MM-DD')
  } else if (d1m == d2m) {
    return d1d + ' 至 ' + d2.format('DD')
  } else if (d1y == d2y) {
    return d1m + ' 至 ' + d2.format('MM月')
  } else {
    return d1y + ' 至 ' + d2y
  }
}
